package network;

import java.io.IOException;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;

import encryption.RSA;

public class CAClient 
{
	String CAPublicKeyMod = "16660929389029777513491376010661864846386518598590828747114307280212346276797736862264273823781716782895220228102793235254065972422358627420236596770567580972450734623153289998436812143618647104958164332163906411132834721217918660848114896205518354121058567144077021708810032950671970787962194347932218623873721392135829367566605455365112177258453999280706768896215609614447030039863576705311382314285644568552745113041949744691640354243517032822567748427033282656720035062158879560768410783399411759001912667180312290156520697932472386497579388628055886825296655161688083527680248008372064735780109946479620258696883";
	String CAPublicKeyExp = "65537";
	
	public CAClient()
	{
		
	}
	
	public String CAClientFlow() throws NoSuchAlgorithmException, IOException
	{
		Client.client.sendMessage(Operations.CA + "");
		
	//	String CA = new String(Client.client.receiveRSAMessage(), "ISO-8859-1");
		String CA = Client.client.receiveMessage();
		
		Client.client.sendMessage("");
		String[] inFromServer = CA.split(" ");
		
		for(int i=0; i<inFromServer.length; i++)
		{
			System.out.println(inFromServer[i]);
		}
		
		CertificateForCA certificate = new CertificateForCA(inFromServer[0], inFromServer[1], inFromServer[2], inFromServer[3]);
		System.out.println(certificate);
		
		String hash = CertificateForCA.HashedCertificate(certificate.toString());
		
		
		String encryptedCertificate = new String(Client.client.receiveRSAMessage() , "ISO-8859-1");
		
		System.out.println("Recveived Encrypted Text");
		
		String decryptedHashedCertificate = new RSA().rsadecrypt(encryptedCertificate, CAPublicKeyMod, CAPublicKeyExp);
		System.out.println(decryptedHashedCertificate);
		
 		if(hash.equals(decryptedHashedCertificate ))
		{
			System.out.println("CAClient: Both are equal, returning public key");
			return certificate.getPublicKey();
		}
		else
		{
			System.out.println("Not Equal");
		}
		
		return null;
	}
}
